<?
require 'global.php';
require(ROOT."root/controller/upfile.php");
include(ROOT."common/class_image_resize.php");

$express_company=new DB(Texpress_company,$conn);
$shipment_method=new DB(Tshipment_method,$conn);
$sales_order=new DB(Tsales_order,$conn);
$sales_order_detail=new DB(Tsales_order_detail,$conn);
$address_book=new DB(Taddress_book,$conn);
$express_company=new DB(Texpress_company,$conn);
$url='old_order';
// 错误集 error array
$errors=array();

$action=isset($_POST["action"])?clean($_POST["action"]):'you guess'; // 数据库操作: CURD 
if($action=='update'){ // sql动作: 更新, 用于上传凭据
	$arr=array();
	$tem_ref_no=$_POST['ref_no'];
	if(isset($_POST['payment_ref_no']))$arr['payment_ref_no']=$_POST['payment_ref_no'];
	if(isset($_POST['payer_id']))$arr['payer_id']=$_POST['payer_id'];
	if(isset($_POST['payer']))$arr['payer']=$_POST['payer'];
	$arr['order_status']=2;
	$arr['paid_time']=date('Y-m-d H:i:s'); // 提交时间
	if($_FILES["cpic"]["size"] > 0){
		$picdir=ROOT.'upload/';
		$pic=upfile($_FILES["cpic"]);
		$image_resize=new Image_resize($picdir.$pic,600,600,0,$picdir.$pic);
		
		$arr['receipt_doc']=$pic;
		$tem=$sales_order->get_row($tem_ref_no,'ref_no');
		$old_doc=ROOT.'upload/'.$tem['receipt_doc'];
		@unlink($old_doc);
	}
	try{
		$sales_order->update($arr,'ref_no',$tem_ref_no);
		//$errors[]=array('type'=>'nothing','url'=>$url.'.php?code='.$tem_ref_no);
		wheel($url.'.php?code='.$tem_ref_no); exit;
	}
	catch(Exception $e){
		$errors[]=array('message'=>WEB_UPDATE_FAILED,'type'=>'alert','url'=>$url.'.php');
		//yell('Failed, please retry.',$url.'.php');
	}
}

// 是国外的还是国内的
$overseas=0;
$ref_no='';
if(isset($_GET["code"])) $ref_no=clean($_GET["code"]);
if($ref_no == '') { header('location:./'); exit; }

// 测试加减库存函数
//$product->reduce_stock_by_order($ref_no);
//exit;

$info=$sales_order->get_row($ref_no,'ref_no');
$province_id=$info['province'];
$order_status=$code_array['order_status'][$info['order_status']]['name_'.$lang];
$order_currency_sign=$arrcurrency[$info['currency_id']]['sign'];
$order_currency_code=$arrcurrency[$info['currency_id']]['code'];
$tem_currency_id=$info['currency_id'];

//钱包余额
$customer_info = $customer->get_row(decrypt($_COOKIE['customer_id']));

// 是否可以继续线上付款 can continue pay online
$can_pay_online=0;
$order_status_id=$info['order_status'];
$payment_method_id=$info['payment_method_id'];
// 未付款状态且付款方法已选择在线方式的, 可以继续付款
if($payment_method_id!=5 && $payment_method_id!=6 && $order_status_id<2) $can_pay_online=1;

// $in_array_product
$in_array_product=array();
$arr_product=array();
$tem_items=$sales_order_detail->get_rows(1024,'sales_order_id',$info['id']);
foreach($tem_items as $val) $in_array_product[]=$val['product_id'];
// $arr_product
$where=array();
$where[]=array('name'=>'id','val'=>$in_array_product,'opn'=>'in');
$tem=$product->get_rows_advanced(1024,$where);
foreach($tem as $val) $arr_product[$val['id']]=$val;

// cart - fake cart. 虚拟购物车
$arr_cart=array();
foreach ($tem_items as $val){
	$arr_cart[]=array(
		'pid'=>$val['product_id'],
		'var'=>$val['product_variation_id'],
		'qty'=>$val['quantity'],
		'unit_price'=>$val['unit_price'],
	);
}

// 是否全部为虚拟产品
$are_virtual = $info['is_virtual'];

// payment method 支付方式
$arr_payment=array();
$tem=$payment_method->get_rows(32,'is_enabled',1,'seq_no');
if($order_status_id>1){
	foreach ($tem as $val){
		if($val['id']==$info['payment_method_id'])$arr_payment[$val['id']]=$val;
	}
}
else {
	foreach ($tem as $val){
    	$arr_payment[$val['id']]=$val;
	}
	// 根据货币 取消不支持的支付方式
	// cny 人民币, paypal 不支持
	if($tem_currency_id==2) { if(isset($arr_payment[3])) unset($arr_payment[3]); }
	// 非人民币, 支付宝/财付通/银联在线不支持
	else {
	    if(isset($arr_payment[1])) unset($arr_payment[1]);
	    if(isset($arr_payment[2])) unset($arr_payment[2]);
	    if(isset($arr_payment[4])) unset($arr_payment[4]);
	}
	if($are_virtual) unset($arr_payment[5]); // 如果全部为虚拟商品, 不能使用货到付款方式
}

// shipment method 寄送方式
$arr_shipment=array();
$tem=$shipment_method->get_rows(32,'is_enabled',1,'seq_no');
foreach ($tem as $val){
	if($val['id']==$info['shipment_method_id']) $arr_shipment[$val['id']]=$val;
}
// shipment company 快递公司
$arr_express=array();
$tem=$express_company->get_rows(32,'is_enabled',1,'seq_no');
foreach ($tem as $val){
	if($val['id']==$info['express']) $arr_express=$val;
}

// pre($arr_shipment); exit;


// SEO 设置 seo tags
$title=WEB_ORDER_INFO.'-'.$ref_no.' - '.$config['SITE_COMPANY_'.LANG];
$kwd=$title;
$dpn=$kwd;

// view 加载视图
require VIEW.$url.'.php';

// end of file
